package cn.kgc.controller;

import cn.kgc.pojo.Pdm;
import cn.kgc.service.PdmService;
import cn.kgc.until.WjhUpload;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author: 王建恒
 * @date: 2022/3/1
 * @description:
 */
@Controller
public class WjhController {

    @Resource
    private PdmService pdmservice;

    /**
     * 跳转人事复核页面,数据跳转
     * @return
     */
    @RequestMapping("/renshifuhe/{id}")
    public String renshifuhe(@PathVariable Integer id,
                             Model model){
        List<Pdm> pdms = pdmservice.fuhPdm(id);
        model.addAttribute("pdms",pdms);
        return "yemian/renshi/renshifuhe";
    }


    /**
     * 跳转人事变更页面,数据跳转
     * @return
     */
    @RequestMapping("/renshibiangeng/{id}")
    public String renshibiangenbge(@PathVariable Integer id,
                                   Model model){
        List<Pdm> pdms = pdmservice.fuhPdm(id);
        model.addAttribute("pdms",pdms);
        return "yemian/renshi/renshibiangeng";
    }


    /**
     * 跳转人事明细页面,数据跳转
     * @return
     */
    @RequestMapping("/renshimingxi/{id}")
    public String renshimingxi(@PathVariable Integer id,
                               Model model){
        List<Pdm> pdms = pdmservice.fuhPdm(id);
        for (Pdm ooo: pdms
        ) {
            System.out.println("明细打印数据----------------------------------------"+ooo);
        }
        model.addAttribute("pdms",pdms);
        return "yemian/renshi/renshimingxi";
    }

    /**
     *跳转人事管理页面
     * @return
     */
    @RequestMapping("/renshiwelcome")
    public String dengji(){
        return "renshidanganguanli";
    }



    /**
     * 人事管理页面需要的数据查询
     * @return
     */
    @ResponseBody
    @RequestMapping("/renshichaxun")
    public String renshi(@RequestParam Integer pageSize,
                         @RequestParam Integer currentPage,
                         @RequestParam String fileCoding,
                         @RequestParam String checkStatus,
                         @RequestParam String fstatus,
                         @RequestParam String startDate,
                         @RequestParam String endDate){

//        System.out.println("档案编号"+fileCoding);
//        System.out.println("审核状态"+checkStatus);
//        System.out.println("档案状态"+fstatus);
//        System.out.println("开始时间"+startDate);
//        System.out.println("结束时间"+endDate);

        PageHelper.startPage(currentPage, pageSize);
        Page<Pdm> userList = pdmservice.selPdmQuanbu(fileCoding, checkStatus, fstatus, startDate, endDate);
        //userList.forEach(System.out::println);
        PageInfo<Pdm> pageInfo = new PageInfo<Pdm>(userList);


        return JSON.toJSONStringWithDateFormat(pageInfo,"yyyy-MM-dd");
    }



    /**
     * 删除方法
     * @param id
     * @return
     */
    @RequestMapping("/renshishanchu")
    @ResponseBody
    public String renshishanchu(@RequestParam Integer id,
                                @RequestParam String shann,
                                @RequestParam String fstatus){
        boolean shan = pdmservice.delPdm(id,shann,fstatus);
        return JSON.toJSONString(shan);
    }

    /**
     * 永久删除方法
     * @param id
     * @return
     */
    @RequestMapping("/renshiyongjiushanchu")
    @ResponseBody
    public String renshiyongjiushanchu(@RequestParam Integer id){
        boolean shan = pdmservice.renshiyongjiushanchu(id);
        return JSON.toJSONString(shan);
    }

    /**
     * 删除恢复方法
     * @param id
     * @return
     */
    @RequestMapping("/renshishanchuhuifu")
    @ResponseBody
    public String renmshishanchuhuifu(@RequestParam Integer id){
        boolean shan = pdmservice.renshidanganshanchuhuifu(id);
        return JSON.toJSONString(shan);
    }

    /**
     * 人事管理复核查询
     * @param id
     * @return
     */
    @RequestMapping("/renshifuhechaxun")
    @ResponseBody
    public String rsnshifuhe(@Param("id") Integer id){
        List<Pdm> pdms = pdmservice.fuhPdm(id);
        return JSON.toJSONStringWithDateFormat(pdms,"yyyy-MM-dd");
    }

    /**
     * 人事复核驳回
     * @param idd
     * @return
     */
    @RequestMapping("/renshifuhebohui")
    @ResponseBody
    public String renshifuhebohui(Integer idd,
                                  String bohui){
//        System.out.println("-------------------------进入了复核方法");
//        System.out.println("bohui = " + bohui);
//        System.out.println("idd = " + idd);
        Integer id = new Integer(idd);
//        System.out.println("id = " + id);
        boolean fuhe = pdmservice.fuhebohui(id,bohui);
        return JSON.toJSONString(fuhe);
    }

    /**
     * 人事审核通过
     * @param idd
     * @return
     */
    @RequestMapping("/renshishenhetongguo")
    @ResponseBody
    public String renshishenhetongguo(Integer idd,
                                      String bohui){
        Integer id = new Integer(idd);
        boolean fuhe = pdmservice.biangengshenhe(idd, bohui);
        return JSON.toJSONString(fuhe);
    }


//     废弃
//    /**
//     * 人事复核保存
//     * @param
//     * @return
//     */
//    @RequestMapping("/renshifuhebalcun")
//    @ResponseBody
//    public String remnshifuhebaocun(  Integer id,
//                                      String fileCoding,
//                                      String changeTheType,
//                                      String fstatus,
//                                      Integer jobTitle,
//                                      String fjname,
//                                      String fjsex,
//                                      String fjemail,
//                                      String phone,
//                                      String address,
//                                      String idNumber,
//                                      String registered,
//                                      String politicsStatus,
//                                      String school,
//                                      String major,
//                                      String education,
//                                      Integer salaaryStandard,
//                                      String openingBank,
//                                      String cardNo,
//                                      String social,
//                                      String registrant,
//                                      String filecreatedDate,
//                                      String redumes,
//                                      String family,
//                                      String notes,
//                                      String resumeAttachment,
//                                      String checkStatus) throws IOException {
//        System.out.println("复核保存进来了");
//        // 获取原文件名
////        String oldFilename = resumeAttachment.getOriginalFilename();
////        System.out.println("oldFilename==================================="+oldFilename);
////        System.out.println(resumeAttachment.toString());
////        System.out.println("第1个"+resumeAttachment.getOriginalFilename());
////        System.out.println("第2个"+resumeAttachment.getName());
////        System.out.println("第3个"+resumeAttachment.getContentType());
////        System.out.println("第4个"+resumeAttachment.getSize(
////        System.out.println("第5个"+resumeAttachment.getBytes());
//
//        boolean b = pdmservice.fuhePdmbaocun(id, fileCoding, changeTheType, fstatus, jobTitle, fjname, fjsex, fjemail, phone, address, idNumber, registered, politicsStatus, school, major, education, salaaryStandard, openingBank, cardNo, social, registrant, filecreatedDate, redumes, family, notes, resumeAttachment, checkStatus);
//        return JSON.toJSONString(b);
//    }


    /**
     * 上传图片
     */
    @PostMapping("/renshishangchuantupian")
    @ResponseBody
    public Boolean upimg(Integer id,@RequestParam("imggggg") MultipartFile imggggg)throws IOException {
        String upzhaopian = WjhUpload.upload(imggggg);//获得了一个文件路径
        boolean upshangchuan = pdmservice.upshangchuan(id, upzhaopian);
        return upshangchuan;
    }


    /**
     * 上传文件:人事复核审核通过
     * @param pdm
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/renshitijiaoa")
    @ResponseBody
    public Boolean upload(Pdm pdm,
                          @RequestParam("file") MultipartFile file,
                          @RequestParam("imggggg") MultipartFile imggggg) throws IOException {
//        int point = filename.lastIndexOf(".");//得到.前面的file的字符长度
//        ++point;
//        System.out.println("-------------------"+point);
//        String types = filename.substring(point);
//        System.out.println("----------==========---------"+types);

        //获得文件的名称
        String filename=file.getOriginalFilename();

//        System.out.println("+++++============="+filename);

        //照片上传
        String fileimggg=imggggg.getOriginalFilename();
        //路径
        //附件路径
        String jianlifujiam = System.getProperty("user.dir")+"\\src\\main\\resources\\upload\\wjh\\"+filename;
        System.out.println("附件路径+++++============="+jianlifujiam);
        //照片路径
        String zhaopianl = System.getProperty("user.dir")+"\\src\\main\\resources\\upload\\wjh\\"+fileimggg;
        System.out.println("照片路径+++++============="+zhaopianl);


//        String upzhaopian = WjhUpload.upload(imggggg);//获得了一个文件路径

//        if (!types.equals("doc")){
//            System.out.println("上传文件的格式错误！，请输入正确的格式！！");
//            return false;
//        }else if(!types.equals("docx")){
//            System.out.println("上传文件的格式错误！，请输入正确的格式！！");
//            return false;
//        }else if (!types.equals("pdf")){
//            System.out.println("上传文件的格式错误！，请输入正确的格式！！");
//            return false;
//        }

        //调用我们写的工具类
        WjhUpload.upload(file);//储存文件
        WjhUpload.upload(imggggg);//储存文件

        String shenghetongguo = "审核通过";
        pdm.setResumeName(filename);
        pdm.setCheckStatus(shenghetongguo);
        pdm.setResumeAttachment(jianlifujiam);
        pdm.setPhonePath(zhaopianl);
        boolean b = pdmservice.fuhePdmbaocun(pdm);

        return b;    // 文件上传后，发送到js
    }




    /**
     * 上传文件:人事变更通过
     * @param pdm
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/renshibiangenga")
    @ResponseBody
    public Boolean uprenshibian(Pdm pdm,
                                @RequestParam("file") MultipartFile file,
                                @RequestParam("imggggg") MultipartFile imggggg) throws IOException {
//        int point = filename.lastIndexOf(".");//得到.前面的file的字符长度
//        ++point;
//        System.out.println("-------------------"+point);
//        String types = filename.substring(point);
//        System.out.println("----------==========---------"+types);

        //获得文件的名称
        String filename=file.getOriginalFilename();

//        System.out.println("+++++============="+filename);

        //照片上传
        String fileimggg=imggggg.getOriginalFilename();
        //路径
        //附件路径
        String jianlifujiam = System.getProperty("user.dir")+"\\src\\main\\resources\\upload\\wjh\\"+filename;
        System.out.println("附件路径+++++============="+jianlifujiam);
        //照片路径
        String zhaopianl = System.getProperty("user.dir")+"\\src\\main\\resources\\upload\\wjh\\"+fileimggg;
        System.out.println("照片路径+++++============="+zhaopianl);


//        String upzhaopian = WjhUpload.upload(imggggg);//获得了一个文件路径

//        if (!types.equals("doc")){
//            System.out.println("上传文件的格式错误！，请输入正确的格式！！");
//            return false;
//        }else if(!types.equals("docx")){
//            System.out.println("上传文件的格式错误！，请输入正确的格式！！");
//            return false;
//        }else if (!types.equals("pdf")){
//            System.out.println("上传文件的格式错误！，请输入正确的格式！！");
//            return false;
//        }

        //调用我们写的工具类
        WjhUpload.upload(file);//储存文件
        WjhUpload.upload(imggggg);//储存文件

        String shenghetongguo = "变更通过";
        pdm.setResumeName(filename);
        pdm.setCheckStatus(shenghetongguo);
        pdm.setResumeAttachment(jianlifujiam);
        pdm.setPhonePath(zhaopianl);
        boolean b = pdmservice.fuhePdmbaocun(pdm);

        return b;    // 文件上传后，发送到js
    }

}
